home *** CD-ROM | disk | FTP | other *** search
/ Multimedia Jumpstart / Multimedia Microsoft Jumpstart Version 1.1a (Microsoft).BIN / develpmt / source / dialogs / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-02  |  6.4 KB  |  204 lines

  1. /*
  2.  * SEARCH.C
  3.  *
  4.  * Code demonstrating the various uses of the FindText and ReplaceText
  5.  * common dialogs.
  6.  *
  7.  * Copyright (c)1992 Microsoft Corporation, All Right Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  * One Microsoft Way
  12.  * Redmond, WA  98052
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  70750,2344
  16.  * Fax       :  (206)936-7329
  17.  */
  18.  
  19. #include <windows.h>
  20. #include <commdlg.h>
  21. #include <dlgs.h>
  22. #include <memory.h>
  23. #include "dialogs.h"
  24.  
  25.  
  26. HWND            hDlgFind;               //FindText modeless dialog handle
  27. FINDREPLACE     frFind;                 //Persistent structure for FindText
  28. char            szFind[256];            //Search string for FindText
  29.  
  30. HWND            hDlgReplace;            //ReplaceText modeless dialog handle
  31. FINDREPLACE     frReplace;              //Persistent structure for ReplaceText
  32. char            szReplaceFind[256];     //Search string for ReplaceText
  33. char            szReplace[256];         //Replace string for ReplaceText
  34.  
  35.  
  36.  
  37.  
  38.  
  39. /*
  40.  * LSearchMessageHandler
  41.  *
  42.  * Purpose:
  43.  *  Processes the FINDMSGSTRING registered message sent from the
  44.  *  find and replace modeless dialogs.  Various bits in pFR->Flags
  45.  *  instruct us what to do, that is, terminate the dialog, find
  46.  *  the next occurance in the search, replace an occurance, or
  47.  *  replace all occurances.  Simply put, these flags indicate which
  48.  *  button the user pressed.
  49.  *
  50.  * Parameters:
  51.  *  hWnd            HWND of the parent window.
  52.  *  pFR             LPFINDREPLACE pointer to either the frFind or
  53.  *                  frReplace globals we used to call FindText or
  54.  *                  ReplaceText, depending on which dialog box
  55.  *                  originated this message.
  56.  *
  57.  * Return Value:
  58.  *  LONG            Value to return from the main window procedure.
  59.  */
  60.  
  61. LONG PASCAL LSearchMessageHandler(HWND hWnd, LPFINDREPLACE pFR)
  62.     {
  63.     char            szFindMsg[512];
  64.  
  65.     if (FR_DIALOGTERM & pFR->Flags)
  66.         {
  67.         /*
  68.          * The dialog is closing on the Close button.  Therefore we must
  69.          * invalidate the handle we saved from FindText or ReplaceText.
  70.          * Since we have two window handles for these dialogs we have to
  71.          * determine which one to invalidate.  Since the FINDMSGSTRING
  72.          * message does not distinguish the two dialogs, we have stored
  73.          * a non-zero in the FINDREPLACE structure's lCustData for
  74.          * FindText and a zero for ReplaceText.
  75.          */
  76.         if (pFR->lCustData)
  77.             hDlgFind=NULL;
  78.         else
  79.             hDlgReplace=NULL;
  80.  
  81.         return 0L;
  82.         }
  83.  
  84.  
  85.     /*
  86.      * Perform your searching here.  For this example we'll simply report
  87.      * the state of the various flags.  This is really dull code, don't
  88.      * you think?
  89.      */
  90.     lstrcpy(szFindMsg, "Dialog:\t");
  91.     lstrcat(szFindMsg, (0==pFR->lCustData) ? "Replace\n" : "Find\n");
  92.  
  93.     lstrcat(szFindMsg, "Button:\t");
  94.     if (FR_FINDNEXT   & pFR->Flags) lstrcat(szFindMsg, "Find Next\n");
  95.     if (FR_REPLACE    & pFR->Flags) lstrcat(szFindMsg, "Replace\n");
  96.     if (FR_REPLACEALL & pFR->Flags) lstrcat(szFindMsg, "Replace All\n");
  97.  
  98.     lstrcat(szFindMsg, "Up/Down:\t");
  99.     lstrcat(szFindMsg, (FR_DOWN & pFR->Flags) ? "Down\n" : "Up\n");
  100.  
  101.     lstrcat(szFindMsg, "Whole Word:\t");
  102.     lstrcat(szFindMsg, (FR_WHOLEWORD & pFR->Flags) ? "On\n" : "Off\n");
  103.  
  104.     lstrcat(szFindMsg, "Match Case:\t");
  105.     lstrcat(szFindMsg, (FR_MATCHCASE & pFR->Flags) ? "On\n" : "Off\n");
  106.  
  107.     MessageBox(hWnd, szFindMsg, "Find/Replace Message", MB_OK);
  108.     return 0L;
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /*
  118.  * SearchDialogs
  119.  *
  120.  * Purpose:
  121.  *  Invokes the FindText and ReplaceText common dialogs.
  122.  *
  123.  * Parameters:
  124.  *  hWndOwner       HWND to use as the owner of the dialog.
  125.  *  iDialog         WORD indicating which dialog variation to invoke.
  126.  *
  127.  * Return Value:
  128.  *  HWND            Handle to the invoked dialog, NULL on failure.
  129.  */
  130.  
  131. HWND PASCAL SearchDialogs(HWND hWndOwner, WORD iDialog)
  132.     {
  133.     HWND            hDlg=NULL;
  134.  
  135.     /*
  136.      * NOTE:  Since we pass a pointer to the FINDREPLACE structure for
  137.      * a modeless dialog, the structure must persist outside the scope of
  138.      * this function, that is, it must be static or global.  We use a
  139.      * different structure for FindText and ReplaceText since we allow
  140.      * both dialogs to be active at one time.
  141.      */
  142.  
  143.     switch (iDialog)
  144.         {
  145.         case IDM_SEARCHFIND:
  146.             if (hDlgFind)
  147.                 break;
  148.  
  149.             memset(&frFind, 0, sizeof(FINDREPLACE));
  150.             frFind.lStructSize=sizeof(FINDREPLACE);
  151.             frFind.hwndOwner=hWndOwner;
  152.  
  153.             lstrcpy(szFind, "Search String");
  154.             frFind.lpstrFindWhat=szFind;
  155.             frFind.wFindWhatLen=sizeof(szFind);
  156.  
  157.             frFind.Flags=FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD;
  158.  
  159.             /*
  160.              * For our own purposes, use lCustData to distinguish
  161.              * an invocation of Find vs. and invocation or Replace
  162.              * within the message processing for FINDMSGSTRING.  Otherwise
  163.              * there's no way to distinguish where that message originated.
  164.              */
  165.             frFind.lCustData=1L;
  166.  
  167.             hDlgFind=FindText(&frFind);
  168.             hDlg=hDlgFind;
  169.             /*
  170.              * FindText returns as soon as the dialog is up. Therefore we
  171.              * have to manage memory and so forth when we are told it's
  172.              * closing.  Memory for the FINDREPLACE structure and the
  173.              * lpstrFindWhat string both must be global or static.
  174.              */
  175.             break;
  176.  
  177.  
  178.         case IDM_SEARCHREPLACE:
  179.             if (hDlgReplace)
  180.                 break;
  181.  
  182.             memset(&frReplace, 0, sizeof(FINDREPLACE));
  183.             frReplace.lStructSize=sizeof(FINDREPLACE);
  184.             frReplace.hwndOwner=hWndOwner;
  185.  
  186.             lstrcpy(szReplaceFind, "Search String");
  187.             frReplace.lpstrFindWhat=szReplaceFind;
  188.             frReplace.wFindWhatLen=sizeof(szReplaceFind);
  189.  
  190.             lstrcpy(szReplace, "Replace String");
  191.             frReplace.lpstrReplaceWith=szReplace;
  192.             frReplace.wReplaceWithLen=sizeof(szReplace);
  193.  
  194.             frReplace.Flags=FR_WHOLEWORD;
  195.             frReplace.lCustData=0L;
  196.  
  197.             hDlgReplace=ReplaceText(&frReplace);
  198.             hDlg=hDlgReplace;
  199.             break;
  200.         }
  201.  
  202.     return hDlg;
  203.     }
  204.